package com.aos.erp.manager.service.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.common.utils.DateUtils;
import com.aos.erp.manager.dto.DsContractDTO;
import com.aos.erp.manager.enums.DelStatus;
import com.aos.erp.manager.enums.DscTimeType;
import com.aos.erp.manager.mapper.dsc.DsContractMapper;
import com.aos.erp.manager.mapper.dsc.DscFileMapper;
import com.aos.erp.manager.model.dsc.vo.DsContractVo;
import com.aos.erp.manager.model.dsc.vo.DscConditionVo;
import com.aos.erp.manager.model.dsc.DsContract;
import com.aos.erp.manager.model.dsc.DscFile;
import com.aos.erp.manager.service.DsContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class DsContractServiceImpl extends ServiceImpl<DsContractMapper,DsContract> implements DsContractService {

    @Resource
    private DscFileMapper dscFileMapper;

    @Override
    public void saveDsc(DsContractVo dataInfo) throws Exception {

        checkSaveContractData(dataInfo);

        // 保存新合同，获得合同ID
        long dsId = saveContractData(dataInfo);

        // 保存合同文件
        saveContractFile(dsId, dataInfo.getDscFileList());
    }

    @Override
    public void deleteDsc(String dataId) throws Exception {

        if(dataId.isEmpty()) {
            throw new BizException("合同ID不能为空");
        }
        baseMapper.deleteDsc(dataId);
    }

    @Override
    public void updateDsc(DsContractVo dataInfo) throws Exception {

        checkUpdateContractData(dataInfo);

        // 修改合同信息
        baseMapper.updateDsc(dataInfo);
        long dscId = dataInfo.getId();

        // 保存下发组织
        // 删除旧记录
        Condition condition = new Condition(DscFile.class);
        condition.eqFiled("dscId", dscId);
        dscFileMapper.delete(condition.getWrapper());

        // 保存新记录
        saveContractFile(dscId, dataInfo.getDscFileList());
    }

    @Override
    public DsContractDTO getDscDetail(long dataId) {

        // 获取合同详情
        DsContract contractData = baseMapper.selectById(dataId);
        // 获取合同文件列表
        Condition condition = new Condition(DscFile.class);
        condition.eqFiled("dscId", dataId);
        List<DscFile> dscFileList = dscFileMapper.selectList(condition.getWrapper());

        DsContractDTO dscDTO = new DsContractDTO();
        dscDTO.setContractData(contractData);
        dscDTO.setDscFileList(dscFileList);
        return dscDTO;
    }

    @Override
    public PageBean<DsContract> findByPage(DscConditionVo conditionVo) {

        IPageBean<DsContract> page = baseMapper.createBizPage(conditionVo.getPageIndex(), conditionVo.getPageSize());

        Condition condition = new Condition(DsContract.class);
        condition.eqFiled("isDel", DelStatus.del_no);
        if(!conditionVo.getDscCode().isEmpty()) {
            condition.likeFiled("dscCode", conditionVo.getDscCode());
        }

        if(!conditionVo.getDscName().isEmpty()) {
            condition.likeFiled("dscName", conditionVo.getDscName());
        }

        if(!conditionVo.getDscCustomerName().isEmpty()) {
            condition.likeFiled("dscCustomerName", conditionVo.getDscCustomerName());
        }

        if(!conditionVo.getDscSignName().isEmpty()) {
            condition.likeFiled("dscSignName", conditionVo.getDscSignName());
        }

        if(conditionVo.getTimeType() == DscTimeType.dsc_sign) {
            // 合同签订时间
            if(!conditionVo.getFromTime().isEmpty()) {
                condition.ge("dscSignTime", conditionVo.getFromTime());
            }
            if(!conditionVo.getToTime().isEmpty()) {
                condition.le("dscSignTime", conditionVo.getToTime());
            }
        } else if(conditionVo.getTimeType() == DscTimeType.dsc_start) {
            // 合同开始时间
            if(!conditionVo.getFromTime().isEmpty()) {
                condition.ge("dscBeginTime", conditionVo.getFromTime());
            }
            if(!conditionVo.getToTime().isEmpty()) {
                condition.le("dscBeginTime", conditionVo.getToTime());
            }
        } else if(conditionVo.getTimeType() == DscTimeType.dsc_end) {
            // 合同截止时间
            if(!conditionVo.getFromTime().isEmpty()) {
                condition.ge("dscEndTime", conditionVo.getFromTime());
            }
            if(!conditionVo.getToTime().isEmpty()) {
                condition.le("dscEndTime", conditionVo.getToTime());
            }
        }
        return (PageBean<DsContract>) baseMapper.selectPage(page, condition.getWrapper());
    }

    /**
     * 保存通知信息
     * @param dataInfo
     * @return
     */
    private long saveContractData(DsContractVo dataInfo) {

        Date nowDate = new Date();
        DsContract dscData = new DsContract();
        BeanUtils.copyProperties(dataInfo, dscData);
        dscData.setDscCode("HT" + DateUtils.getXXTimeString());
        dscData.setIsDel(DelStatus.del_no);
        dscData.setCreateUser(dataInfo.getOptUser());
        dscData.setCreateTime(nowDate);
        dscData.setUpdateUser(dataInfo.getOptUser());
        dscData.setUpdateTime(nowDate);
        this.save(dscData);
        return dscData.getId();
    }

    /**
     * 保存合同文件
     * @param dscId
     * @param dscFileList
     */
    private void saveContractFile(long dscId, List<DscFile> dscFileList) {
        for (DscFile dscFile : dscFileList) {
            dscFile.setDscId(dscId);
            dscFileMapper.insert(dscFile);
        }
    }


    private void checkContractData(DsContractVo params) throws Exception {
//        if(params.getDscCode().isEmpty()) {
//            throw new BizException("合同编号不能为空");
//        }
        if(params.getDscName().isEmpty()) {
            throw new BizException("合同名称不能为空");
        }
        if(params.getDscName().length() > 100) {
            throw new BizException("合同名称长度不能超过100个字符");
        }
        if(params.getDscCustomerName().isEmpty()) {
            throw new BizException("客户名称不能为空");
        }
        if(params.getDscCustomerName().length() > 100) {
            throw new BizException("客户名称长度不能超过100个字符");
        }
        if(params.getDscContactsName().isEmpty()) {
            throw new BizException("联系人不能为空");
        }
        if(params.getDscContactsName().length() > 100) {
            throw new BizException("联系人长度不能超过100个字符");
        }
        if(params.getDscContactsPhone().isEmpty()) {
            throw new BizException("联系电话不能为空");
        }
        if(params.getDscSignName().isEmpty()) {
            throw new BizException("签订人不能为空");
        }
        if(params.getDscSignName().length() > 100) {
            throw new BizException("签订人长度不能超过100个字符");
        }
        if(params.getDscSignTime() == null) {
            throw new BizException("签订时间不能为空");
        }
        if(params.getDscBeginTime() == null) {
            throw new BizException("合同开始时间不能为空");
        }
        if(params.getDscEndTime() == null) {
            throw new BizException("合同截止时间不能为空");
        }
        if(params.getDscFileList().size() == 0) {
            throw new BizException("合同文件不能为空");
        }
    }

    private void checkSaveContractData(DsContractVo params) throws Exception {
        checkContractData(params);
    }

    private void checkUpdateContractData(DsContractVo params) throws Exception {

        if(params.getId() == 0) {
            throw new BizException("合同ID不能为空");
        }
        checkContractData(params);
    }
}